package dtrs.corba.server.semaphores;

import dtrs.corba.server.common.ShowDTO;
import dtrs.corba.utilities.DtrsConstants;

/**
 * To control the thead to acces the rows in the hasMap
 * @author Mary
 *
 */
public class ManageMapShowSemaphore extends Thread {
	
	private DtrsMapShowID mapShows;
	private String keyMapShow;
	private Integer numberOfTicketsShow;
	private int operationInRow;
	
	private Integer ticketsLeft;
	
	public ShowDTO showi;

	
	//constructor in case of check operation
	public ManageMapShowSemaphore(DtrsMapShowID mapShowsP, String keyMapShowP, Integer numberOfTickets,
			int operationInRowP, ShowDTO showObj){

				mapShows = mapShowsP;
				keyMapShow = keyMapShowP;
				numberOfTicketsShow = numberOfTickets;	
				operationInRow = operationInRowP;
				showi = showObj;
				}

	//Constructor in case of reservation
	public ManageMapShowSemaphore(DtrsMapShowID mapShowsP, String keyMapShowP, Integer numberOfTickets,
									int operationInRowP){
		
		mapShows = mapShowsP;
		keyMapShow = keyMapShowP;
		numberOfTicketsShow = numberOfTickets;	
		operationInRow = operationInRowP;

		
		
	}
	
	
	

	
	public void run(){
		
		try{
			
			if(operationInRow == DtrsConstants.ROW_ACTION_GET_TICKETS_AVAILABLES){				
					mapShows.getTicketsLeftShowId(keyMapShow, ticketsLeft,showi);		
				
			}else if (operationInRow == DtrsConstants.ROW_ACTION_UPDATE_RESERVE_TICKETS ||
					operationInRow == DtrsConstants.ROW_ACTION_UPDATE_CANCEL_TICKETS){
				
					mapShows.updateRowShowId(keyMapShow, numberOfTicketsShow, operationInRow, showi);
			}

		}finally{
			mapShows.releaseRow(keyMapShow);
		}
		
	}




	public ShowDTO getShowi() {
		return showi;
	}




	public void setShowi(ShowDTO showi) {
		this.showi = showi;
	}

	

	
	
}

